<template>
  <div>
    <div v-if="treeData.length == 0">
      <el-button type="text" @click.native="handleAdd">新增分组</el-button>
    </div>
    <div v-else>
      <el-input
        size="mini"
        placeholder="输入关键字进行过滤"
        v-model="filterText"
        style="margin-bottom: 10px"
      />
      <el-tree
        :data="treeData"
        node-key="code"
        :props="treeProps"
        default-expand-all
        ref="groupTree"
        :filter-node-method="filterNode"
        :highlight-current="true"
        @node-click = "handleNodeClick"
      >
        <span
          class="custom-tree-node"
          slot-scope="{ node, data }"
          @mouseover="mouseover(data)"
          @mouseleave="mouseout(data)"
        >
          <span>
            <i
              v-if="node.children && node.children.length > 0"
              class="el-icon-folder"
              style="margin-right: 10px"
            />
            <i v-else class="el-icon-document" style="margin-right: 10px" />{{
              node.label
            }}</span
          >
          <div v-show="data.dropdownShow" @click.stop="() => {}">
            <el-dropdown placement="right-start" trigger="click">
              <i class="el-icon-s-operation"></i>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item
                  icon="el-icon-plus"
                  @click.native="addPeerTree(node, data)"
                  >新增同级</el-dropdown-item
                >
                <el-dropdown-item
                  icon="el-icon-circle-plus"
                  @click.native="addChildrenTree(node, data)"
                  >新增下级</el-dropdown-item
                >
                <el-dropdown-item
                  icon="el-icon-edit-outline"
                  @click.native="updateTree(node, data)"
                  >修改</el-dropdown-item
                >
                <el-dropdown-item
                  icon="el-icon-delete-solid"
                  @click.native="delTree(node, data)"
                  >删除</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </span>
      </el-tree>
    </div>

    <!-- 添加或修改分组信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标识" prop="code">
          <el-input v-model="form.code" placeholder="请输入标识" :disabled='this.form.id  != null'/>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="上级标识" prop="parentCode">
          <treeselect
            v-model="form.parentCode"
            :options="groupOptions"
            :normalizer="normalizer"
            placeholder="选择上级分组"
          />
        </el-form-item>
        <el-form-item label="排序" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {
  listGroup,
  getGroup,
  delGroup,
  addGroup,
  updateGroup,
} from "@/api/lc/lcDatamodelGroup";
export default {
  name: "DemoTreeSide",
  components: { Treeselect },
  data() {
    return {
      filterText: "",
      treeData: [
        {
          code: "1",
          title: "a",
          label: "b",
        },
      ],
      treeProps: {
        children: "children",
        label: "title",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "请输入标识", trigger: "blur" },
          {
            min: 3,
            max: 100,
            message: "长度在 3 到 5 个字符",
            trigger: "blur",
          },
        ],
        title: [
          { required: true, message: "请输入名称", trigger: "blur" },
          {
            min: 3,
            max: 100,
            message: "长度在 3 到 5 个字符",
            trigger: "blur",
          },
        ],
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单分组选项
      groupOptions: [],
    };
  },

  created() {
    this.getGroupList();
  },
  watch: {
    filterText(val) {
      this.$refs.groupTree.filter(val);
    },
  },
  methods: {
    /**
     * 按钮点击事件
     */
    handleNodeClick(data, node) {
      this.$emit('select', data, node)
    },
    getGroupList(selectCode) {
      listGroup().then((response) => {
        this.treeData = this.handleTree(response.data, "code", "parentCode");
        if (selectCode) {
          this.$nextTick(() => {
            this.$refs.groupTree.setCurrentKey(selectCode);
          });
        }
      });
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.title.indexOf(value) !== -1;
    },
    mouseover(data) {
      // 移入
      this.$set(data, "dropdownShow", true);
    },
    mouseout(data) {
      // 移除
      this.$set(data, "dropdownShow", false);
    },
    addPeerTree(node, data) {
      this.handleAdd(data.parentCode);
    },
    addChildrenTree(node, data) {
      this.handleAdd(data.code);
    },
    updateTree(node, data) {
      this.handleUpdate(data.id);
    },
    delTree(node, data) {
      this.handleDelete(data);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ID: null,
        CODE: null,
        TITLE: null,
        parentCode: null,
        orderNum: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        REMARK: null,
      };
      this.resetForm("form");
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.ID);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd(parentCode) {
      this.reset();
      this.open = true;
      this.title = "添加分组信息";
      this.form.parentCode = parentCode;
      listGroup().then((response) => {
        this.groupOptions = this.handleTree(response.data, "code", "parentCode");
      });
    },
    /** 修改按钮操作 */
    handleUpdate(id) {
      this.reset();
      getGroup(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改分组信息";
        listGroup().then((response) => {
          this.groupOptions = response.data;
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(data) {
      this.$modal
        .confirm('是否确认删除分组信息为"' + data.title + '"的数据项？')
        .then(function () {
          return delGroup(data.code);
        })
        .then(() => {
          this.getGroupList(data.parentCode);
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateGroup(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getGroupList(this.form.code);
            });
          } else {
            addGroup(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getGroupList(this.form.code);
            });
          }
        }
      });
    },
    /** 转换options数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.code,
        code: node.code,
        label: node.title,
        children: node.children,
      };
    },
  },
};
</script>
<style scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 10px;
  height: 100%;
}
.el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
  background-color: #409eff;
  color: white;
}
</style>
